﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WmWebSystem.Models;
using WmWebSystem.Service;

namespace WmWebSystem.DbHelper
{
    public static class UserAccountHelper
    {
        public static void AddNewAccountToCustomers(string name, string email,string teltphone,string logo)
        {
            WmDb mydb = new WmDb();
            DateTime time = DateTime.Now;

            UserAccount cus = new UserAccount();

            cus.Name = name;
            cus.Email = email;
            cus.Telephone = teltphone;
            cus.Logo = logo;
            cus.Status = 1;
            cus.AddTime = time;
            cus.Account = 0;
            try
            {
                mydb.UserAccounts.Add(cus);
                mydb.SaveChanges();
                //将记录写入日志
                string p1 = "Add new to Customers, The Name:" + name;
                WriteLog.WriteSysLog(p1);
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常
                WriteLog.WriteSysLog(ex.ToString());
            }


        }

        public static UserAccount FindAccountByName(string user)
        {
            WmDb mydb = new WmDb();
            UserAccount customer = (from c in mydb.UserAccounts
                                 where c.Name == user
                                 orderby c.AddTime descending
                                 select c).FirstOrDefault();
            return customer;

        }

        public static IEnumerable<UserAccount> FindAll()
        {
            WmDb mydb = new WmDb();
            var customer = from c in mydb.UserAccounts
                                   orderby c.AddTime
                                   select c;
            return customer;
        }

        public static IEnumerable<UserAccount> FindMusicianInCustomer() 
        {
            WmDb mydb = new WmDb();
            var musicians = (from c in mydb.UserAccounts
                             from p in mydb.Projects
                             where c.Name == p.Author && p.Status == 2
                             orderby p.AddTime descending
                             select c).Take(8);
            return musicians;
            
           

        }

        public static IEnumerable<UserAccount> FindAccountBySearch(string name, string stime, string etime, int status)
        {
            WmDb mydb = new WmDb();
            if (name == null && stime == null && etime == null && status == 0)
            {
                //0000
                var customers = from c in mydb.UserAccounts
                               orderby c.AddTime descending
                               select c;
                return customers;
            }
            else if (name != null && stime == null && etime == null && status == 0)
            {
                //1000
                var customers = from c in mydb.UserAccounts
                               where c.Name.Contains(name)
                               select c;
                return customers;
            }
            else if (name == null && stime != null && etime == null && status == 0)
            {
                //0100
                DateTime time = Convert.ToDateTime(stime);
                var customers = from c in mydb.UserAccounts
                               where c.AddTime >= time
                               select c;
                return customers;
            }
            else if (name == null && stime == null && etime != null && status == 0)
            {
                //0010
                DateTime time = Convert.ToDateTime(etime);
                var customers = from c in mydb.UserAccounts
                               where c.AddTime <= time
                               select c;
                return customers;
            }
            else if (name == null && stime == null && etime == null && status != 0)
            {
                //0001
                var customers = from c in mydb.UserAccounts
                               where c.Status == status
                               select c;
                return customers;
            }
            else if (name != null && stime != null && etime == null && status == 0)
            {
                //1100
                DateTime time = Convert.ToDateTime(stime);
                var customers = from c in mydb.UserAccounts
                               where c.Name.Contains(name) && c.AddTime >= time
                               select c;
                return customers;
            }
            else if (name != null && stime == null && etime != null && status == 0)
            {
                //1010
                DateTime time = Convert.ToDateTime(etime);
                var customers = from c in mydb.UserAccounts
                               where c.Name.Contains(name) && c.AddTime <= time
                               select c;
                return customers;
            }
            else if (name != null && stime == null && etime == null && status != 0)
            {
                //1001
                var customers = from c in mydb.UserAccounts
                               where c.Name.Contains(name) && c.Status == status
                               select c;
                return customers;
            }
            else if (name == null && stime != null && etime != null && status == 0)
            {
                //0110
                DateTime startTime = Convert.ToDateTime(stime);
                DateTime endTime = Convert.ToDateTime(etime);
                var customers = from c in mydb.UserAccounts
                               where c.AddTime >= startTime && c.AddTime <= endTime
                               select c;
                return customers;
            }
            else if (name == null && stime != null && etime == null && status != 0)
            {
                //0101
                DateTime startTime = Convert.ToDateTime(stime);
                var customers = from c in mydb.UserAccounts
                               where c.AddTime >= startTime && c.Status == status
                               select c;
                return customers;
            }
            else if (name == null && stime == null && etime != null && status != 0)
            {
                //0011
                DateTime endTime = Convert.ToDateTime(etime);
                var customers = from c in mydb.UserAccounts
                               where c.Status == status && c.AddTime <= endTime
                               select c;
                return customers;
            }
            else if (name == null && stime != null && etime != null && status != 0)
            {
                //0111
                DateTime startTime = Convert.ToDateTime(stime);
                DateTime endTime = Convert.ToDateTime(etime);
                var customers = from c in mydb.UserAccounts
                               where c.AddTime >= startTime && c.AddTime <= endTime && c.Status == status
                               select c;
                return customers;
            }
            else if (name != null && stime == null && etime != null && status != 0)
            {
                //1011
                DateTime endTime = Convert.ToDateTime(etime);
                var customers = from c in mydb.UserAccounts
                               where c.Name.Contains(name) && c.AddTime <= endTime && c.Status == status
                               select c;
                return customers;
            }
            else if (name != null && stime != null && etime == null && status != 0)
            {
                //1101
                DateTime startTime = Convert.ToDateTime(stime);
                var customers = from c in mydb.UserAccounts
                               where c.Name.Contains(name) && c.AddTime >= startTime && c.Status == status
                               select c;
                return customers;
            }
            else if (name != null && stime != null && etime != null && status == 0)
            {
                //1110
                DateTime startTime = Convert.ToDateTime(stime);
                DateTime endTime = Convert.ToDateTime(etime);
                var customers = from c in mydb.UserAccounts
                               where c.Name.Contains(name) && c.AddTime >= startTime && c.AddTime <= endTime
                               select c;
                return customers;
            }
            else
            {
                //1111
                DateTime startTime = Convert.ToDateTime(stime);
                DateTime endTime = Convert.ToDateTime(etime);
                var customers = from p in mydb.UserAccounts
                                where p.Name.Contains(name) && p.AddTime >= startTime && p.AddTime <= endTime && p.Status == status
                                select p;
                return customers;
            }

        }

        public static bool IsExistUserName(string name) 
        {
            WmDb mydb = new WmDb();
            int count = (from c in mydb.UserAccounts
                         where c.Name == name
                         select c).Count();
            if (count > 0)
                return true;   //存在 返回true
            else
                return false;
        }

    }
}